package com.sunday.volunteer.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.adapter.AddressAdapter;
import com.sunday.volunteer.entity.Address;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Pual's PC on 2016/10/17.
 */
public class AddressActivity extends BaseActivity {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.swipfresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.title_text)
    TextView title_text;

    private LinearLayoutManager layoutManager;
    private AddressAdapter adapter;
    private List<Address> list;
    private boolean isAddress = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        title_text.setText("收货地址管理");

        isAddress = getIntent().getExtras() == null?false :true;
        list = new ArrayList<>();
        adapter = new AddressAdapter(list ,R.layout.item_address_edit);
        adapter.setOnClickListener(onClickListener);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        swipeRefreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .drawable(R.drawable.shape_divider)
                .build());
        recyclerView.setAdapter(adapter);

        handlerRecyclerView();
        getData();
    }

    private void handlerRecyclerView() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }


    private void getData() {
        swipeRefreshLayout.setRefreshing(false);
        Call<ResultDO<List<Address>>> call = HttpClient.getHttpAdapter().list(BaseApp.getInstance().getMemberId());
        call.enqueue(new Callback<ResultDO<List<Address>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Address>>> call, Response<ResultDO<List<Address>>> response) {
                if(response.body() == null ||isFinish){
                    return;
                }
                if(response.body().getCode() == 0 && response.body().getResult() != null){
                    list.clear();
                    list.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(Call<ResultDO<List<Address>>> call, Throwable t) {

            }
        });

    }
    public void AddAddress(View view){
        BaseUtil.openActivity(mContext ,AddAddressActivity.class);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = (int) v.getTag();
            Address address =list.get(position);
            switch (v.getId()){
                case R.id.address_edit:
                    intent = new Intent(mContext , AddAddressActivity.class);
                    intent.putExtra(BaseUtil.ITEM , address);
                    startActivity(intent);
                    break;
                case R.id.address_del:
                    BaseUtil.showDialog(mContext , "是否删除" , new BaseUtil.BaseUtilClickListener() {
                        @Override
                        public void doRightbtn() {
                            deleteAddress(position);
                        }
                    });
                    break;
                case R.id.to_address:
                    if(isAddress){
                        intent = new Intent();
                        intent.putExtra(BaseUtil.ITEM , list.get((int) v.getTag()));
                        setResult(RESULT_OK , intent);
                        finish();
                    }
                    break;
            }
        }
    };


    private void deleteAddress(final int position){
        Call<ResultDO> call = HttpClient.getHttpAdapter().delete(list.get(position).getId());
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if(response.body() == null ||isFinish){
                    return;
                }
                if(response.body().getCode() == 0){
                    getData();
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });
    }
}
